package cn.com.controller;

import cn.com.Authorization;
import cn.com.beans.PResource;
import cn.com.beans.PRole;
import cn.com.service.PResourceService;
import cn.com.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/role")
public class RoleContorller implements BaseController{

    @Resource
    private RoleService roleService;

    @Resource
    private PResourceService resourceService;

    @GetMapping
    public String home() {
        return getModelName()+"/index";
    }

    @Override
    public String getModelName() {
        return "role";
    }


    @GetMapping(headers = "X-Requested-With=XMLHttpRequest")
    @ResponseBody
    @Authorization(url = "/role",authority = "r",methods = RequestMethod.GET)
    public Map<String,Object> findAll(PRole role){
        List<PRole> resources = roleService.findAll(role);
        Map<String,Object> result = new HashMap<>();
        result.put("data",resources);
        result.put("code",0);
        result.put("count",resources.size());
        result.put("msg","查询成功");
        return result;
    }
    @GetMapping({"/edit","/edit/{id}"})
    @Authorization(url = "/role",authority = "r",methods = RequestMethod.GET)
    public String editor(@PathVariable(required = false) Integer id, @RequestParam(defaultValue = "-1") Integer pid, Model model){
        PRole role = null;
        if(id != null){
            role = this.roleService.findByid(id);

        }
        model.addAttribute("role",role);
        return getModelName()+"/editor";
    }
    @PutMapping
    @ResponseBody
    @Authorization(url = "/role",authority = "c",methods = RequestMethod.PUT)
    public Map<String,Object> save(PRole role){
        Map<String,Object> results = new HashMap<>();
        if(role.getId()!= null){
            if(roleService.update(role)){
                results.put("code",0);
                results.put("msg","修改成功");
            }else{
                results.put("code",1);
                results.put("msg","修改失败");
            }
        }else{
            if(roleService.insert(role)){
                results.put("code",0);
                results.put("msg","新增成功");
            }else{
                results.put("code",1);
                results.put("msg","新增失败");
            }
        }
        return results;
    }
    @DeleteMapping
    @ResponseBody
    @Authorization(url = "/role",authority = "d",methods = RequestMethod.DELETE)
    public Map<String,Object> delete(@RequestParam("id") Integer[] ids){
        Map<String,Object> resoult = new HashMap<>();
        if(roleService.deleteByIds(ids)){
            resoult.put("code",0);
            resoult.put("msg","删除成功");
        }else {
            resoult.put("code",1);
            resoult.put("msg","删除失败");
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return resoult;
    }
    @GetMapping("/authorization/{id}")
    @Authorization(url = "/role/authorization",authority="r",methods = RequestMethod.GET)
    public String authorization(@PathVariable Integer id, ModelMap model){
        model.addAttribute("id",id);
        return getModelName()+"/authorization";
    }
    @PutMapping("/authorization")
    @ResponseBody
    @Authorization(url="/role/authorization",authority="c",methods = RequestMethod.PUT)
    public Map<String,Object> authorization(@RequestParam Integer id, @RequestParam("resourceIds[]") Integer[] resourcceIds){
        Map<String,Object> results = new HashMap<>();
        try {
            this.resourceService.authorization(id,resourcceIds);
            results.put("code",0);
            results.put("msg","授权成功");
        }catch (Exception e){
            e.printStackTrace();
            results.put("code",1);
            results.put("msg","授权失败:"+e.getMessage());
        }
        return results;
    }

    @GetMapping(path = "/authorization/{id}",headers="X-Requested-With=XMLHttpRequest")
    @ResponseBody
    @Authorization(url ="/role/authorization",authority="c",methods = RequestMethod.GET)
    public Map<String,Object> authorization(@PathVariable Integer id){
        List<PResource> resources = resourceService.findAll(null);
        PRole account = this.roleService.findByid(id);
        List<PResource> accountResource = account.getResources();
        Map<String,Object> results = new HashMap<>();
        results.put("code",0);
        results.put("msg","查询成功");
        Map<String,Object> data = new HashMap<>();
        data.put("list",resources);
        List<Integer> ids = accountResource.stream().map(res -> res.getId()).collect(Collectors.toList());
        data.put("checkedId",ids);
        results.put("data",data);
        return results;
    }
}
